
const
    autoprefixer = require('autoprefixer'),
    path = require('path'),
    webpack = require('webpack'),
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    CaseSensitivePathPlugin = require('case-sensitive-paths-webpack-plugin'),
    InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin'),
    eslintFormatter = require('react-dev-utils/eslintFormatter'),
    ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin'),
    getClientEnvironment = require('./env'),
    paths = require('./paths'),
    publicPath = '/',
    publicUrl = '',
    env = getClientEnvironment(publicUrl)
;

const exportsResult = {
    devtool: 'cheap-module-source-map',
    entry: [
        require.resolve('./polyfills'),
        require.resolve('react-dev-utils/webpackHotDevClient'),
        paths.appIndexJs
    ],
    output: {
        pathinfo: true,
        filename: 'dist/js/bundle.js',
        chunkFilename: 'static/js/[name].chunk.js',
        publicPath: publicPath,
        devtoolModuleFilenameTemplate: info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')
    },
    resolve: {
        modules: ['node_modules', paths.appNodeModules].concat(
            process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
        ),
        extensions: ['.web.js', '.mjs', '.js', '.json', '.tsx', 'ts', '.web.jsx', '.jsx'],
        alias: {
            'babel-runtime': path.dirname(require.resolve('babel-runtime/package.json')),
            'react-native': 'react-native-web',
            'bootstrap': path.dirname(require.resolve('react-bootstrap/lib'))
        },
        plugins: [
            new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson])
        ]
    },
    context: __dirname,
    module: {
        strictExportPresence: true,
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader'
            }, {
                test: /\.(js|jsx|mjs)$/,
                enforce: 'pre',
                use: [
                    {
                        loader: require.resolve('eslint-loader'),
                        options: {
                            formatter: eslintFormatter,
                            eslintPath: require.resolve('eslint'),
                            baseConfig: {
                                extends: [require.resolve('eslint-config-react-app')]
                            },
                            ignore: false,
                            useEslintrc: false,
                        }
                    }
                ],
                include: paths.appSrc
            }, {
                enforce: "pre", 
                test: /\.js$/, 
                loader: "source-map-loader"
            }, {
                oneOf: [
                    {
                        test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
                        loader: require.resolve('url-loader'),
                        options: {
                            limit: 10000,
                            name: 'static/media/[name].[hash:8].[ext]'
                        }
                    }, {
                        test: /\.(js|jsx|mjs)$/,
                        include: paths.appSrc,
                        exclude: /(node_modules|bower_components)/,
                        loader: require.resolve('babel-loader'),
                        options: {
                            cacheDirectory: true,
                            // babelrc: false,
                            // presets: [require.resolve('babel-preset-react-app')],
                            // presets: ['env', 'react'],
                            // plugins: ['transform-decorators-legacy','transform-decorators', 'transform-object-rest-spread']
                        }
                    }, {
                        test: /\.css$/,
                        use: [
                            require.resolve('style-loader'),
                            {
                                loader: require.resolve('css-loader'),
                                options: {
                                    importLoaders: 1,
                                    modules: true,
                                    localIdentName: '[name]__[local]--[hash:base64:5]' // 指定css的类名格式
                                }
                            }, {
                                loader: require.resolve('postcss-loader'),
                                options: {
                                    plugins: [autoprefixer]
                                }
                            }
                        ]
                    }, {
                        test: /\.scss$/,
                        use: ['style-loader','css-loader','sass-loader']
                    }, {
                        exclude: [/\.js$/, /\.html$/, /\.json$/],
                        loader: require.resolve('file-loader'),
                        options: {
                            name: 'static/media/[name].[hash:8].[ext]'
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new InterpolateHtmlPlugin(env.raw),
        new HtmlWebpackPlugin({
            inject: true,
            template: paths.appHtml
        }),
        new webpack.NamedChunksPlugin(),
        new webpack.DefinePlugin(env.stringified),
        new webpack.HotModuleReplacementPlugin(),
        new CaseSensitivePathPlugin(),
        new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ],
    node: {
        dgram: 'empty',
        fs: 'empty',
        net: 'empty',
        tls: 'empty',
        child_process: 'empty'
    },
    performance: {
        hints: false
    }
};

module.exports = exportsResult;